rm(list=ls())
d <- read.csv("data-yougov.csv")


# FigureB3 -----------------------------------

RfT <- lm_robust(treatT ~ age + political_attention + factor(income) + factor(education) + factor(region) + factor(religion) + factor(social_grade), se_type="HC1", data=d[!is.na(d$findTory),])
RfL <- lm_robust(treatL ~ age + political_attention + factor(income) + factor(education) + factor(region) + factor(religion) + factor(social_grade), se_type="HC1", data=d[!is.na(d$findLabour),])
RvT <- lm_robust(treatT ~ age + political_attention + factor(income) + factor(education) + factor(region) + factor(religion) + factor(social_grade), se_type="HC1", data=d[!is.na(d$voteTory),])
RvL <- lm_robust(treatL ~ age + political_attention + factor(income) + factor(education) + factor(region) + factor(religion) + factor(social_grade), se_type="HC1", data=d[!is.na(d$voteLabour),])

RfT.d <- data.frame(summary(RfT)$coefficients[rownames(summary(RfT)$coefficients)!="(Intercept)",])
RfL.d <- data.frame(summary(RfL)$coefficients[rownames(summary(RfL)$coefficients)!="(Intercept)",])

randcheck1 <- ggplot(RfT.d, aes(as.factor(rownames(RfT.d)), Estimate)) + 
  geom_pointrange(aes(ymin=CI.Lower, ymax=CI.Upper), shape=16, size=1.5, lwd=1.5) +
  geom_hline(yintercept = 0, color="red", lwd=1.2) + 
  theme_minimal() + ylab("(a) Randomization Check: Predictors of Treatment 1") + xlab(" ") + 
  theme(axis.title = element_text(size=14),
        axis.text = element_text(size=12)) +
  coord_flip() + ylim(-1,1)

randcheck2 <- ggplot(RfL.d, aes(as.factor(rownames(RfL.d)), Estimate)) + 
  geom_pointrange(aes(ymin=CI.Lower, ymax=CI.Upper), shape=16, size=1.5, lwd=1.5) +
  geom_hline(yintercept = 0, color="red", lwd=1.2) + 
  theme_minimal() + ylab("(b) Randomization Check: Predictors of Treatment 2") + xlab(" ") + 
  theme(axis.title = element_text(size=14),
        axis.text = element_text(size=12)) + 
  scale_x_discrete(labels=NULL) + 
  coord_flip() + ylim(-1,1) 
ggarrange(randcheck1, randcheck2)
ggsave("FigureB3a.pdf", width = 15, height = 6, units = "in")


RvT.d <- data.frame(summary(RvT)$coefficients[rownames(summary(RvT)$coefficients)!="(Intercept)",])
RvL.d <- data.frame(summary(RvL)$coefficients[rownames(summary(RvL)$coefficients)!="(Intercept)",])

randcheck3 <- ggplot(RvT.d, aes(as.factor(rownames(RvT.d)), Estimate)) + 
  geom_pointrange(aes(ymin=CI.Lower, ymax=CI.Upper), shape=16, size=1.5, lwd=1.5) +
  geom_hline(yintercept = 0, color="red", lwd=1.2) + 
  theme_minimal() + ylab("(a) Randomization Check: Predictors of Treatment 1") + xlab(" ") + 
  theme(axis.title = element_text(size=14),
        axis.text = element_text(size=12)) +
  coord_flip() + ylim(-1,1)

randcheck4 <- ggplot(RvL.d, aes(as.factor(rownames(RvL.d)), Estimate)) + 
  geom_pointrange(aes(ymin=CI.Lower, ymax=CI.Upper), shape=16, size=1.5, lwd=1.5) +
  geom_hline(yintercept = 0, color="red", lwd=1.2) + 
  theme_minimal() + ylab("(b) Randomization Check: Predictors of Treatment 2") + xlab(" ") + 
  theme(axis.title = element_text(size=14),
        axis.text = element_text(size=12)) + 
  scale_x_discrete(labels=NULL) + 
  coord_flip() + ylim(-1,1) 
ggarrange(randcheck3, randcheck4)
ggsave("FigureB3b.pdf", width = 15, height = 6, units = "in")




# Figure 3 ----------------------------------------------------

findT.Tory   <- lm_robust(findTory ~ factor(orderA) + factor(setABC) + treatT + age + political_attention + factor(income) + factor(education) + factor(region) + factor(religion) + factor(social_grade), se_type="HC1", data=d[d$ToryPartisanVSLabour==1,])
findT.Labour <- lm_robust(findTory ~ factor(orderA) + factor(setABC) + treatT + age + political_attention + factor(income) + factor(education) + factor(region) + factor(religion) + factor(social_grade), se_type="HC1", data=d[d$ToryPartisanVSLabour==0,])

findT <- c(findT.Tory$coefficients["treatT"], findT.Labour$coefficients["treatT"])
se    <- c(findT.Tory$std.error["treatT"], findT.Labour$std.error["treatT"])
party <- c("Tory", "Labour")
data1 <- data.frame(findT, se, party)

findL.Tory   <- lm_robust(findLabour ~ factor(orderA) + factor(setABC) + treatL + age + political_attention + factor(income) + factor(education) + factor(region) + factor(religion) + factor(social_grade), se_type="HC1", data=d[d$ToryPartisanVSLabour==1,])
findL.Labour <- lm_robust(findLabour ~ factor(orderA) + factor(setABC) + treatL + age + political_attention + factor(income) + factor(education) + factor(region) + factor(religion) + factor(social_grade), se_type="HC1", data=d[d$ToryPartisanVSLabour==0,])

findL <- c(findL.Tory$coefficients["treatL"], findL.Labour$coefficients["treatL"])
se    <- c(findL.Tory$std.error["treatL"], findL.Labour$std.error["treatL"])
party <- c("Tory", "Labour")
data2 <- data.frame(findL, se, party)


expT <- ggplot(data = data1, aes(x = findT, y = as.factor(party) )) +
  geom_pointrange(aes(xmin=findT-1.96*se, xmax=findT+1.96*se, colour=party), size=2, lwd=2) + 
  scale_colour_manual(values=c("skyblue","tomato"),labels=c("Labour","Tory"), name="Party") + 
  xlab("(a) Effect of Most Similar Tory Treatment on Identification")+ ylab("Voter Partisanship")  + theme_minimal() + 
  theme(axis.text = element_text(size=14),
        axis.text.x = element_text (margin=margin(0,0,18,0)),
        axis.title.y = element_text(angle=90, hjust=.5, margin = margin(0,40,0,0)),
        axis.title = element_text(hjust=0.25, size=17),
        legend.position = "none") + xlim(-.25,.65) + 
  geom_vline(xintercept = 0, linetype="solid", color = "red", size=.4) 


expL <- ggplot(data = data2, aes(x = findL, y = as.factor(party))) +
  geom_pointrange(aes(xmin=findL-1.96*se, xmax=findL+1.96*se, colour=party), size=2, lwd=2) + 
  scale_colour_manual(values=c("skyblue","tomato"),labels=c("Labour","Tory"), name="Party") + 
  xlab("(b) Effect of Most Similar Labour Treatment on Identification")+ ylab(" ")  + theme_minimal() + 
  theme(axis.text = element_text(size=14), 
        axis.text.x = element_text (margin=margin(0,0,18,0)),
        axis.title = element_text(hjust=0.25, size=17),
        legend.position = "none") + xlim(-.25,.65) + 
  geom_vline(xintercept = 0, linetype="solid", color = "red", size=.4) 

ggarrange(expT, expL)
ggsave("Figure3.pdf", width = 15, height = 6, units = "in")






# Figure 4 ------------------------------------------------------------------

voteT.Tory   <- lm_robust(voteTory ~ factor(orderA) + factor(setABC) + treatT + age + political_attention + factor(income) + factor(education) + factor(region) + factor(religion) + factor(social_grade), se_type="HC1", data=d[d$ToryPartisanVSLabour==1,])
voteT.Labour <- lm_robust(voteTory ~ factor(orderA) + factor(setABC) + treatT + age + political_attention + factor(income) + factor(education) + factor(region) + factor(religion) + factor(social_grade), se_type="HC1", data=d[d$ToryPartisanVSLabour==0,])

voteT <- c(voteT.Tory$coefficients["treatT"], voteT.Labour$coefficients["treatT"])
se   <- c(voteT.Tory$std.error["treatT"], voteT.Labour$std.error["treatT"])
party <- c("Tory", "Labour")
data3 <- data.frame(voteT, se, party)

voteL.Tory   <- lm_robust(voteLabour ~ factor(orderA) + factor(setABC) + treatL + age + political_attention + factor(income) + factor(education) + factor(region) + factor(religion) + factor(social_grade), se_type="HC1", data=d[d$ToryPartisanVSLabour==1,])
voteL.Labour <- lm_robust(voteLabour ~ factor(orderA) + factor(setABC) + treatL + age + political_attention + factor(income) + factor(education) + factor(region) + factor(religion) + factor(social_grade), se_type="HC1", data=d[d$ToryPartisanVSLabour==0,])

voteL <- c(voteL.Tory$coefficients["treatL"], voteL.Labour$coefficients["treatL"])
se   <- c(voteL.Tory$std.error["treatL"], voteL.Labour$std.error["treatL"])
party <- c("Tory", "Labour")
data4 <- data.frame(voteL, se, party)


expT <- ggplot(data = data3, aes(x = voteT, y = as.factor(party) )) +
  geom_pointrange(aes(xmin=voteT-1.96*se, xmax=voteT+1.96*se, colour=party), size=2, lwd=2) + 
  scale_colour_manual(values=c("skyblue","tomato"),labels=c("Labour","Tory"), name="Party") + 
  xlab("(a) Effect of Most Similar Tory Treatment on Voting")+ ylab("Voter Partisanship")  + theme_minimal() + 
  theme(axis.text = element_text(size=14),
        axis.text.x = element_text (margin=margin(0,0,18,0)),
        axis.title.y = element_text(angle=90, hjust=.5, margin = margin(0,40,0,0)),
        axis.title = element_text(hjust=0.25, size=17),
        legend.position = "none") + xlim(-.45,.5) + 
  geom_vline(xintercept = 0, linetype="solid", color = "red", size=.4) 

expL <- ggplot(data = data4, aes(x = voteL, y = as.factor(party) )) +
  geom_pointrange(aes(xmin=voteL-1.96*se, xmax=voteL+1.96*se, colour=party), size=2, lwd=2) + 
  scale_colour_manual(values=c("skyblue","tomato"),labels=c("Labour","Tory"), name="Party") + 
  xlab("(b) Effect of Most Similar Labour Treatment on Voting")+ ylab(" ")  + theme_minimal() + 
  theme(axis.text = element_text(size=14), 
        axis.text.x = element_text (margin=margin(0,0,18,0)),
        axis.title = element_text(hjust=0.25, size=17),
        legend.position = "none") + xlim(-.4,.5) + 
  geom_vline(xintercept = 0, linetype="solid", color = "red", size=.4) 

ggarrange(expT, expL)
ggsave("Figure4.pdf", width = 15, height = 6, units = "in")



